﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Soft.Net
{
    [TypeConverter(typeof(IPTypeConverter))]
    [Serializable]
    public struct IP : IEquatable<IP>, IComparable<IP>
    {

        public byte ItemA
        {
            get
            {
                return this.mItemA;
            }
            set
            {
                if (this.mItemA != value)
                {
                    this.mItemA = value;
                    this.reset();
                }
            }
        }


        public byte ItemB
        {
            get
            {
                return this.mItemB;
            }
            set
            {
                if (this.mItemB != value)
                {
                    this.mItemB = value;
                    this.reset();
                }
            }
        }

        public byte ItemC
        {
            get
            {
                return this.mItemC;
            }
            set
            {
                if (this.mItemC != value)
                {
                    this.mItemC = value;
                    this.reset();
                }
            }
        }


        public byte ItemD
        {
            get
            {
                return this.mItemD;
            }
            set
            {
                if (this.mItemD != value)
                {
                    this.mItemD = value;
                    this.reset();
                }
            }
        }

        public string Text
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.mText))
                {
                    this.reset();
                }
                return this.mText;
            }
            set
            {
                string text = value;
                if (text == null)
                {
                    throw new ArgumentNullException("value");
                }
                value = text.Trim();
                if (ToolsClass.RegexTest("^\\d{1,3}(\\.\\d{1,3}){3}$", value))
                {
                    string[] array = value.Split(new char[]
                    {
                        '.'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    this.mItemA = (byte)array[0].ConvertTo(0, true);
                    this.mItemB = (byte)array[1].ConvertTo(0, true);
                    this.mItemC = (byte)array[2].ConvertTo(0, true);
                    this.mItemD = (byte)array[3].ConvertTo(0, true);
                    this.reset();
                    return;
                }
                throw new FormatException("参数 value 表示的IP地址格式有错误");
            }
        }

        public override bool Equals(object obj)
        {
            return obj != null && obj is IP && this.Equals((IP)obj);
        }

        public override int GetHashCode()
        {
            return this.mText.GetHashCode();
        }

        private void reset()
        {
            this.mText = string.Format("{0}.{1}.{2}.{3}", new object[]
            {
                this.mItemA,
                this.mItemB,
                this.mItemC,
                this.mItemD
            });
        }

        public override string ToString()
        {
            return this.Text;
        }

        public static IP Parse(string value)
        {
            return new IP
            {
                Text = value
            };
        }

        public static bool TryParse(string value, out IP ip)
        {
            ip = default(IP);
            try
            {
                ip.Text = value;
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static IP TryParse(string value, IP defValue)
        {
            IP result;
            if (IP.TryParse(value, out result))
            {
                return result;
            }
            return defValue;
        }

        public bool Equals(IP other)
        {
            return this.ItemA == other.ItemA && this.ItemB == other.ItemB && this.ItemC == other.ItemC && this.ItemD == other.ItemD;
        }

        public int CompareTo(IP other)
        {
            int num = this.mItemA.CompareTo(other.ItemA);
            if (num == 0)
            {
                num = this.mItemB.CompareTo(other.ItemB);
            }
            if (num == 0)
            {
                num = this.mItemC.CompareTo(other.ItemC);
            }
            if (num == 0)
            {
                num = this.mItemD.CompareTo(other.ItemD);
            }
            return num;
        }

        public static explicit operator IP(string value)
        {
            return IP.Parse(value);
        }

        public static bool operator ==(IP a, IP b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(IP a, IP b)
        {
            return !a.Equals(b);
        }

        private byte mItemA;

        private byte mItemB;

        private byte mItemC;

        private byte mItemD;

        private string mText;

        public static readonly IP Empty = new IP
        {
            mItemA = 0,
            mItemB = 0,
            mItemC = 0,
            ItemD = 0
        };
    }
}
